<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

class CheckRecord extends Model
{
    protected $name = 'check_record';
    
    protected $autoWriteTimestamp = false;
    
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';
    
    // 关联铁路
    public function railway()
    {
        return $this->belongsTo(Railway::class, 'railway_id');
    }
    
    // 关联起始站点
    public function start()
    {
        return $this->belongsTo(RailwayStation::class, 'start_id');
    }
    
    // 关联结束站点
    public function end()
    {
        return $this->belongsTo(RailwayStation::class, 'end_id');
    }

    public function station()
    {
        return $this->belongsTo(RailwayStation::class, 'end_id');
    }

    public function checkItem()
    {
        return $this->hasMany(CheckItem::class, 'check_record_id');
    }
    
    // 获取创建时间
    public function getCreateTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s', intval($value)) : '';
    }
    
    // 获取更新时间
    public function getUpdateTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s', intval($value)) : '';
    }
    
    // 获取删除时间
    public function getDeleteTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s', intval($value)) : '';
    }

    public function getUserNameAttr($value,$data)
    {
        return User::where(['id' => $data['user_id']])->value('name');
    }

    public function getTypeNameAttr($value,$data)
    {
        return CheckType::where(['id' => $data['railway_id']])->value('name');
    }

    public function getProgressTextAttr($value,$data)
    {
        $array = [
            1 => '已到站',
            2 => '检修中',
            3 => '检修完成',
        ];
        return $array[$data['progress']] ?? '';
    }

    public function getDirectionTextAttr($value,$data)
    {
        $array = [
            1 => '下行',
            2 => '上行',
        ];
        return $array[$data['direction']] ?? '';
    }
}
